(ns samber.routes.questions
  (:require
    [reitit.ring.coercion :as coercion]
    [reitit.coercion.spec :as spec-coercion]
    [reitit.ring.middleware.muuntaja :as muuntaja]
    [reitit.ring.middleware.multipart :as multipart]
    [reitit.ring.middleware.parameters :as parameters]
    [samber.middleware.formats :as formats]
    [samber.middleware.exception :as exception]
    [samber.middleware :as middleware]
    [ring.util.http-response :refer :all]
    [clojure.java.io :as io]
    [spec-tools.data-spec :as ds]

    [clojure.tools.logging :as log]
    [samber.services.question.question :as question]))

(def questions-routes
  ["/samber"
   {:swagger {:tags ["question"]}}
   ["/question"
    {:post {:summary "new."
            :parameters {:body {:content string?, :detail string?}}
            :responses {200 {:body {:code int? :msg string?, (ds/opt :errors) any?
                                                           , (ds/opt :data) any?}}}
            :middleware [middleware/wrap-restricted]
            :handler (fn [{{:keys [body]} :parameters token :identity}]
                       {:status 200
                        :body {:code 0 :msg "success"
                               :data (question/create body (:id token))}})}}]
   ["/question/:id"
    {:get {:summary "get."
           :parameters {:path {:id integer?}}
           :responses {200 {:body {:code int? :msg string?, (ds/opt :data) any?}}}
           :handler (fn [{{{:keys [id]} :path} :parameters}]
                      {:status 200
                       :body {:code 0 :msg "success"
                              :data (question/fetch id)}})}}]])
